package com.shijunhao.graduation.service.scheduled;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.bean.Keyword;
import com.shijunhao.graduation.domain.entity.BusinessConnect;
import com.shijunhao.graduation.domain.entity.BusinessNote;
import com.shijunhao.graduation.service.business.IBusinessConnectService;
import com.shijunhao.graduation.service.business.IBusinessNoteService;
import com.shijunhao.graduation.service.business.impl.SimilarAnalyzer;
import com.shijunhao.graduation.utils.TopKQueue;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: shijunhao
 * @date: 2022/5/8 4:43
 * @description:
 */
@Service
public class SuggestScheduleService {

    @Resource
    private IBusinessNoteService businessNoteService;
    @Resource
    private SimilarAnalyzer similarAnalyzer;
    @Resource
    private IBusinessConnectService businessConnectService;

    private Logger logger = LoggerFactory.getLogger(SuggestScheduleService.class);

    @Scheduled(cron = "0 0 23 * * ?")
    @PreDestroy
    @Transactional(rollbackFor = Exception.class)
    public void updateNote(){
        update(1);
        logger.info("更新相关文章推荐成功");
    }

    @Scheduled(cron = "0 0 23 * * ?")
    @PreDestroy
    @Transactional(rollbackFor = Exception.class)
    public void updateInfo(){
        update(0);
        logger.info("更新相关问贴推荐成功");
    }

    public void update(Integer type){
        //查询当天变更过的文章信息
        List<BusinessNote> notes = businessNoteService.selectByCondition(Wrappers.lambdaQuery(BusinessNote.class)
                .eq(BusinessNote::getType,type).last("and DAY(update_date) = DAY(now())"));
        List<BusinessNote> all = businessNoteService.selectByCondition(Wrappers.lambdaQuery(BusinessNote.class).eq(BusinessNote::getType,type));
        List<List<Keyword>> allKeyWords = new ArrayList<>();
        //获取关键词
        for(BusinessNote note:all){
            List<Keyword> keywords = JSON.parseArray(note.getKeywords(), Keyword.class);
            allKeyWords.add(keywords);
        }
        //轮询更新相关文章
        for(BusinessNote note : notes){
            List<Keyword> keywords = JSON.parseArray(note.getKeywords(), Keyword.class);
            TopKQueue topKQueue = new TopKQueue(10);
            for(int i=0;i<all.size();i++){
                if(all.get(i).getId().equals(note.getId()))
                    continue;
                List<Keyword> compare = allKeyWords.get(i);
                double similar = similarAnalyzer.similar(keywords, compare);
                topKQueue.add(new Pair(all.get(i).getId(),similar));
            }
            List<Long> res = topKQueue.res();
            List<BusinessConnect> save = new ArrayList<>();
            for(Long l:res){
                BusinessConnect businessConnect = new BusinessConnect();
                businessConnect.setOriginalId(note.getId());
                businessConnect.setTargetId(l);
                save.add(businessConnect);
            }
            businessConnectService.deleteByCondition(Wrappers.lambdaQuery(BusinessConnect.class).eq(BusinessConnect::getOriginalId,note.getId()));
            businessConnectService.insertBatch(save);
        }
    }
}
